package other.edan.newpaas.order;

import com.rkhd.platform.sdk.data.model.Order;
import com.rkhd.platform.sdk.data.model.OrderProduct;
import com.rkhd.platform.sdk.exception.AsyncTaskException;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.XObject;
import com.rkhd.platform.sdk.service.FutureTaskService;
import com.rkhd.platform.sdk.task.FutureTask;
import com.rkhd.platform.sdk.trigger.DataResult;
import com.rkhd.platform.sdk.trigger.Trigger;
import com.rkhd.platform.sdk.trigger.TriggerRequest;
import com.rkhd.platform.sdk.trigger.TriggerResponse;
import other.edan.newpaas.utils.HCXObjectService;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description 新增订单明细后：赋值主表 测试卡 字段
 * @Author 黎炎松
 * 2025-02-28 11:52
 */
public class SetCardAmount implements Trigger {

    private static final Logger LOGGER = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "新增订单明细后：测试卡 赋值 ==> ";

    @Override
    public TriggerResponse execute(TriggerRequest request) throws ScriptBusinessException {
        LOGGER.debug(LOGGER_MSG + "触发开始");
        List<XObject> dataList = request.getDataList();
        List<DataResult> dataResultList = new ArrayList<>();
        List<XObject> objectList = new ArrayList<>();
        for (XObject xObject : dataList) {
            objectList.add(xObject);
            dataResultList.add(new DataResult(true, "", xObject));
        }

        OrderProduct orderProduct = (OrderProduct) objectList.get(0);
        getCardAmount(orderProduct.getOrderId());

        return new TriggerResponse(true, "处理完成", dataResultList);
    }

    public static void main(String[] args) throws Exception {
        new SetCardAmount().getCardAmount(3702108161628773L);
    }

    /**
     * 获取价格
     * @param orderId
     */
    public void getCardAmount(Long orderId) {
        try {
            LOGGER.info(LOGGER_MSG + "开始处理");

            // 获取订单明细 避免 xoql 有延迟情况
            //String sql = "select id,productSeries2__c,priceTotal from orderProduct where productSeries2__c in (163) and orderId = " + orderId;
            //List<JSONObject> orderProductList = XoqlService.instance().query(sql).getRecords();

            String sql = "select id,productSeries2__c,priceTotal from orderProduct where orderId = " + orderId;
            List<OrderProduct> orderProductList = HCXObjectService.queryAll(sql);
            double priceTotal = orderProductList.stream().filter(v -> Objects.equals(v.getProductSeries2__c(), 163)).map(v -> new BigDecimal(v.getPriceTotal()))
                    .collect(Collectors.reducing(BigDecimal.ZERO, BigDecimal::add)).doubleValue();
            LOGGER.info(LOGGER_MSG + "priceTotal = " + priceTotal);

            Order upOrer = new Order();
            upOrer.setId(orderId);
            upOrer.setCardAmount__c(priceTotal);
            HCXObjectService.update(upOrer);

            LOGGER.info(LOGGER_MSG + "处理成功");
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(LOGGER_MSG + "处理异常: " + e.getMessage());
        }
    }



}
